package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.adapter.JobKnowledgeDragAdapter;
import com.jp.knowledge.model.PositionKnowlegeMode;

import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/7/12.
 */

public class JobknowledgeDragDialog extends Dialog implements View.OnClickListener {

	private Context mContext;
	private View contentView;
	private TextView sureBtn;
	private RecyclerView sortView;
	private TextView levelPrimaryBtn;
	private TextView levelIntermediateBtn;
	private TextView levelAdvancedBtn;

	private List<PositionKnowlegeMode> datas;
	private JobKnowledgeDragAdapter adapter;
	private ItemTouchHelper itemTouchHelper;

	public JobknowledgeDragDialog(Context context) {
		super(context, R.style.custom_idalgo);
		mContext = context;
		contentView = getLayoutInflater().inflate(R.layout.drag_headline_sort, null);
		setContentView(contentView);
		setCanceledOnTouchOutside(true);
		Window dialogWindow = this.getWindow();
		dialogWindow.setGravity(Gravity.CENTER);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		lp.width = (int) (DensityUtil.getScreenWidth()*0.92);
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		init();
	}

	private void init(){
		datas = new ArrayList<>();

		sureBtn = (TextView) findViewById(R.id.sure);
		sortView = (RecyclerView) findViewById(R.id.activity_sort_head);
		levelPrimaryBtn = (TextView) findViewById(R.id.level_primary);
		levelIntermediateBtn = (TextView) findViewById(R.id.level_intermediate);
		levelAdvancedBtn = (TextView) findViewById(R.id.level_advanced);

		levelPrimaryBtn.setTag(1);
		levelIntermediateBtn.setTag(2);
		levelAdvancedBtn.setTag(3);

		sureBtn.setOnClickListener(this);
		levelPrimaryBtn.setOnClickListener(this);
		levelIntermediateBtn.setOnClickListener(this);
		levelAdvancedBtn.setOnClickListener(this);

		sortView.setHasFixedSize(true);
		sortView.setLayoutManager(new GridLayoutManager(mContext, 3));
		adapter = new JobKnowledgeDragAdapter(mContext, datas);
		sortView.setAdapter(adapter);
		itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
			@Override
			public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
				return makeMovementFlags(ItemTouchHelper.UP
						| ItemTouchHelper.DOWN
						| ItemTouchHelper.LEFT
						| ItemTouchHelper.RIGHT, 0);
			}
			@Override
			public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
				int fromPosition = viewHolder.getAdapterPosition();
				int toPosition = target.getAdapterPosition();
				if(itemDragListener != null)
					itemDragListener.onMove(fromPosition, toPosition);
//				PositionKnowlegeMode moveModel = datas.remove(fromPosition);
//				datas.add(toPosition, moveModel);
				adapter.notifyItemMoved(fromPosition, toPosition);
				return true;
			}
			@Override
			public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {}
			@Override
			public boolean isLongPressDragEnabled() {
				return false;
			}
			@Override
			public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
				super.clearView(recyclerView, viewHolder);
				if(itemDragListener != null)
					itemDragListener.onDragFinish();
			}
		});
		itemTouchHelper.attachToRecyclerView(sortView);
		adapter.setItemTouchListener(new JobKnowledgeDragAdapter.ItemTouchListener() {
			@Override
			public boolean onItemTouch(View v, MotionEvent event, RecyclerView.ViewHolder viewHolder) {
				itemTouchHelper.startDrag(viewHolder);
				return false;
			}
		});
	}

	public void setDatas(List<PositionKnowlegeMode> datas){
		this.datas = datas;
		this.adapter.updateAdapter(this.datas);
	}

	public void show(List<PositionKnowlegeMode> datas, int skillLevel){
		setDatas(datas);
		setLevelBtn(skillLevel);
		super.show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.sure:
				break;
			case R.id.level_primary:
			case R.id.level_intermediate:
			case R.id.level_advanced:
				setLevelBtn((int) v.getTag());
				if(levelSelectedListener != null)
					levelSelectedListener.onLevelSelected((int) v.getTag());
				break;
		}
	}

	private void setLevelBtn(int level){
		levelPrimaryBtn.setBackground(mContext.getResources().getDrawable(R.drawable.btn_rbg_gray));
		levelPrimaryBtn.setTextColor(mContext.getResources().getColor(R.color.gray_deep));
		levelIntermediateBtn.setBackground(mContext.getResources().getDrawable(R.drawable.btn_rbg_gray));
		levelIntermediateBtn.setTextColor(mContext.getResources().getColor(R.color.gray_deep));
		levelAdvancedBtn.setBackground(mContext.getResources().getDrawable(R.drawable.btn_rbg_gray));
		levelAdvancedBtn.setTextColor(mContext.getResources().getColor(R.color.gray_deep));
		TextView btn = null;
		if(level == 1)
			btn = levelPrimaryBtn;
		else if(level == 2)
			btn = levelIntermediateBtn;
		else if(level == 3)
			btn = levelAdvancedBtn;
		if(btn == null)
			return;
		btn.setBackground(mContext.getResources().getDrawable(R.drawable.btn_blue));
		btn.setTextColor(mContext.getResources().getColor(R.color.white));
	}

	private LevelSelectedListener levelSelectedListener;
	public void setLevelSelectedListener(LevelSelectedListener levelSelectedListener){
		this.levelSelectedListener = levelSelectedListener;
	}
	public interface LevelSelectedListener{
		void onLevelSelected(int level);
	}

	private ItemDragListener itemDragListener;
	public void setItemDragListener(ItemDragListener itemDragListener){
		this.itemDragListener = itemDragListener;
	}
	public interface ItemDragListener{
		void onMove(int fromPosition, int toPosition);
		void onDragFinish();
	}
}
